<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

<head th:include="header">
    <title>用户管理-列表</title>
</head>

<body>
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>用户管理</h5>
                </div>
                <div class="ibox-content">
                    <div class="row row-lg">
                        <div class="col-sm-12">
                            <div class="row">
                                <div class="col-lg-2 col-sm-3">
                                    <div class="panel panel-default">
                                        <div class="panel-heading">组织机构</div>
                                        <div class="panel-body dept-tree">
                                            <ul id="deptTree" class="ztree"></ul>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-lg-10 col-sm-9">
                                    <div class="row">
                                        <div class="col-lg-5 col-sm-5">
                                            <div class="row">
                                                <div class="col-lg-8 col-sm-12">
                                                    <input type="text" class="form-control" id="name" name="name" placeholder="登录名/用户名" />
                                                </div>
                                                <div class="col-sm-3">
                                                    <button type="submit" class="btn btn-sm btn-primary" onclick="MgrUser.search()">
                                                        <i class="fa fa-search">搜索</i>
                                                    </button>
                                                </div>
                                                <!--<div class="col-lg-4 col-sm-6">-->
                                                    <!--<input type="text" class="form-control laydate" id="startTime" name="startTime" placeholder="注册开始日期" onclick="laydate({istime:false,format:'YYYY-MM-DD'})"-->
                                                    <!--/>-->

                                                <!--</div>-->
                                                <!--<div class="col-lg-4 col-sm-6">-->
                                                    <!--<input type="text" class="form-control laydate" id="endTime" name="endTime" placeholder="注册结束日期" onclick="laydate({istime:false,format:'YYYY-MM-DD'})"-->
                                                    <!--/>-->
                                                <!--</div>-->
                                            </div>
                                        </div>
                                        <!--<div class="col-lg-2 col-sm-3">-->
                                            <!--<div class="row">-->
                                                <!--<div class="col-lg-12 col-sm-12">-->
                                                    <!--<button type="submit" class="btn btn-sm btn-primary" onclick=" MgrUser.resetSearch()">-->
                                                        <!--<i class="fa fa-fa-trash">重置</i>-->
                                                    <!--</button>-->
                                                <!--</div>-->
                                            <!--</div>-->
                                        <!--</div>-->
                                    </div>
                                    <div class="hidden-xs" id="managerTableToolbar" role="group">
                                        <button shiro:hasPermission="/user/add" class="btn btn-sm btn-primary" type="submit" onclick="MgrUser.openAddMgr()">
                                            <i class="fa fa-pencil"></i>添加</button>
                                        <button shiro:hasPermission="/user/edit" class="btn btn-sm btn-info" type="submit" onclick="MgrUser.openChangeUser()">
                                            <i class="fa fa-paste"></i>修改</button>
                                        <button shiro:hasPermission="/user/delete" class="btn btn-sm btn-danger" type="submit" onclick="MgrUser.delMgrUser()">
                                            <i class="fa fa-cut"></i>删除</button>
                                        <button shiro:hasPermission="/user/reset" class="btn btn-sm btn-primary" type="submit" onclick="MgrUser.resetPwd()">
                                            <i class="fa fa-pencil"></i>重置密码</button>
                                        <button  shiro:hasRole="admin" class="btn btn-sm btn-primary" type="submit" onclick="MgrUser.freezeAccount()">
                                            <i class="fa fa-pencil"></i>冻结</button>
                                        <button shiro:hasRole="admin" class="btn btn-sm btn-primary" type="submit" onclick="MgrUser.unfreeze()">
                                            <i class="fa fa-pencil"></i>解除冻结</button>
                                        <button shiro:hasRole="admin" class="btn btn-sm btn-primary" type="submit" onclick="MgrUser.roleAssign()">
                                            <i class="fa fa-pencil"></i>角色分配</button>
                                    </div>
                                    <table id="managerTable" />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div th:include="footer"></div>
    <script type="text/javascript" th:inline="javascript">
        /**
         * 系统管理--用户管理的单例对象
         */
        var MgrUser = {
            id: "managerTable",//表格id
            seItem: null,		//选中的条目
            table: null,
            layerIndex: -1,
            deptid: 0
        };

        /**
         * 初始化表格的列
         */
        MgrUser.initColumn = function () {
            var columns = [
                { field: 'selectItem', radio: true },
                { title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle' },
                { title: '账号', field: 'loginName', align: 'center', valign: 'middle', sortable: true },
                { title: '姓名', field: 'name', align: 'center', valign: 'middle', sortable: true },
                { title: '角色', field: 'rolesList', align: 'center', valign: 'middle', sortable: true },
                {
                    title: '性别', field: 'sex', align: 'center', valign: 'middle', sortable: true, formatter: function (value, row, index) {
                        switch (value) {
                            case 0:
                                return '男';
                            case 1:
                                return '女';
                        }
                    }
                },
                { title: '邮箱', field: 'email', align: 'center', valign: 'middle', sortable: true },
                { title: '电话', field: 'phone', align: 'center', valign: 'middle', sortable: true },
                {
                    title: '状态', field: 'status', align: 'center', valign: 'middle', sortable: true, formatter: function (value, row, index) {
                        switch (value) {
                            case 0:
                                return '正常';
                            case 1:
                                return '停用';
                        }
                    }
                },
                {
                    title: '用户状态', field: 'userType', align: 'center', valign: 'middle', sortable: true, formatter: function (value, row, index) {
                        if (value == 0) {
                            return "超级用户";
                        } else if (value == 1) {
                            return "企业用户";
                        } else if (value == 2) {
                            return "监管部门";
                        } else {
                            return "未知";
                        }
                    }
                },
                { title: '部门', field: 'organizationName', align: 'center', valign: 'middle', sortable: true },
                { title: '公司', field: 'enterpriseName', align: 'center', valign: 'middle', sortable: true },
                {
                    title: '创建时间', field: 'createTime', align: 'center', valign: 'middle', sortable: true
                },
                {
                    title: '更新时间', field: 'updateTime', align: 'center', valign: 'middle', sortable: true
                }];
            return columns;
        };

        /**
         * 检查是否选中
         */
        MgrUser.check = function () {
            var selected = $('#' + this.id).bootstrapTable('getSelections');
            if (selected.length == 0) {
                hdw.info("请先选中表格中的某一记录！");
                return false;
            } else {
                MgrUser.seItem = selected[0];
                return true;
            }
        };

        /**
         * 点击添加管理员
         */
        MgrUser.openAddMgr = function () {
            var index = layer.open({
                type: 2,
                title: '添加管理员',
                area: ['35%', '90%'], //宽高
                fix: false, //不固定
                maxmin: true,
                content: basePath + 'user/addPage'
            });
            this.layerIndex = index;
        };

        /**
         * 点击修改按钮时
         * @param userId 管理员id
         */
        MgrUser.openChangeUser = function () {
            if (this.check()) {
                var index = layer.open({
                    type: 2,
                    title: '编辑管理员',
                    area: ['35%', '90%'], //宽高
                    fix: false, //不固定
                    maxmin: true,
                    content: basePath + 'user/editPage/' + this.seItem.id
                });
                this.layerIndex = index;
            }
        };

        /**
         * 点击角色分配
         * @param
         */
        MgrUser.roleAssign = function () {
            if (this.check()) {
                var index = layer.open({
                    type: 2,
                    title: '角色分配',
                    area: ['300px', '300px'], //宽高
                    fix: false, //不固定
                    maxmin: true,
                    content: basePath + 'user/roleAssign/' + this.seItem.id
                });
                this.layerIndex = index;
            }
        };

        /**
         * 删除用户
         */
        MgrUser.delMgrUser = function () {
            if (this.check()) {

                var operation = function () {
                    var userId = MgrUser.seItem.id;
                    var ajax = new $ax(basePath + "user/delete", function (data) {
                        if (data.success) {
                            hdw.success("删除成功!");
                        } else {
                            hdw.error("删除失败!" + data.msg + "!");
                        }
                        MgrUser.table.refresh();
                    }, function (data) {
                        hdw.error("删除失败!" + data.responseJSON.message + "!");
                    });
                    ajax.set("userId", userId);
                    ajax.start();
                };

                hdw.confirm("是否删除用户" + MgrUser.seItem.loginName + "?", operation);
            }
        };

        /**
         * 冻结用户账户
         * @param userId
         */
        MgrUser.freezeAccount = function () {
            if (this.check()) {
                var userId = this.seItem.id;
                var ajax = new $ax(basePath + "user/freeze", function (data) {
                    if (data.success) {
                        hdw.success("冻结成功!");
                    } else {
                        hdw.error("冻结失败!" + data.msg + "!");
                    }
                    MgrUser.table.refresh();
                }, function (data) {
                    hdw.error("冻结失败!" + data.responseJSON.message + "!");
                });
                ajax.set("userId", userId);
                ajax.start();
            }
        };

        /**
         * 解除冻结用户账户
         * @param userId
         */
        MgrUser.unfreeze = function () {
            if (this.check()) {
                var userId = this.seItem.id;
                var ajax = new $ax(basePath + "user/unfreeze", function (data) {
                    if (data.success) {
                        hdw.success("解除冻结成功!");
                    } else {
                        hdw.error("解除冻结失败!" + data.msg + "!");
                    }
                    MgrUser.table.refresh();
                }, function (data) {
                    hdw.error("解除冻结失败!");
                });
                ajax.set("userId", userId);
                ajax.start();
            }
        }

        /**
         * 重置密码
         */
        MgrUser.resetPwd = function () {
            if (this.check()) {
                var userId = this.seItem.id;
                parent.layer.confirm('是否重置密码为123456？', {
                    btn: ['确定', '取消'],
                    shade: false //不显示遮罩
                }, function () {
                    var ajax = new $ax(basePath + "user/reset", function (data) {
                        if (data.success) {
                            hdw.success("重置密码成功!");
                        } else {
                            hdw.error("重置密码失败!" + data.msg + "!");
                        }
                    }, function (data) {
                        hdw.error("重置密码失败!");
                    });
                    ajax.set("userId", userId);
                    ajax.start();
                });
            }
        };
        //重置
        MgrUser.resetSearch = function () {
            $("#name").val("");
            $("#startTime").val("");
            $("#endTime").val("");

            MgrUser.search();
        }
        //搜索
        MgrUser.search = function () {
            var queryData = {};

            queryData['deptId'] = MgrUser.deptid;
            queryData['name'] = $("#name").val();
            queryData['startTime'] = $("#startTime").val();
            queryData['endTime'] = $("#endTime").val();
            queryData['sort'] = "id";
            console.log(queryData);

            MgrUser.table.refresh({ query: queryData });
        }

        MgrUser.onClickDept = function (e, treeId, treeNode) {
            MgrUser.deptid = treeNode.id;
            MgrUser.search();
        };

        $(function () {
            var defaultColunms = MgrUser.initColumn();
            var table = new BSTable("managerTable", basePath + "user/dataGrid", defaultColunms);
            table.setPaginationType("server");
            MgrUser.table = table.init();
            var ztree = new $ZTree("deptTree", basePath + "organization/tree");
            ztree.bindOnClick(MgrUser.onClickDept);
            ztree.init();


        });
    </script>
</body>

</html>